/*****************************
 *@file: HeroNormalAttack
 *@author: 陈吕唐
 *@desc: 英雄射击普通攻击
 *@date: 2024-03-20	17:23
 *****************************/
import { Node, NodePool, Prefab, _decorator, instantiate } from 'cc';
import { CharacterEvent } from '../CharacterEvent';
import GameUtils from '../GameUtils';
import { BaseBullet } from '../bullet/BaseBullet';
import IBaseBulletValue from '../bullet/IBaseBulletValue';
import { BaseAnimationSkill } from './BaseAnimationSkill';
const { ccclass, property } = _decorator;

@ccclass('HeroNormalAttack')
export class HeroNormalAttack extends BaseAnimationSkill {
    /****************************************************************************************************************
     *Readonly 常量
     ****************************************************************************************************************/

    /****************************************************************************************************************
     *Property 定义变量
     ****************************************************************************************************************/
    /**
     * 对象池
     */
    private pool: NodePool = new NodePool();
    /***************************************************************
     *PropertyUI 引用的控件
     ***************************************************************/

    @property({ group: { name: 'UI', id: '1' }, type: Prefab, tooltip: `英雄普通攻击子弹预制体` })
    private prefab: Prefab = undefined!;



    /****************************************************************************************************************
     * Lifecycle 生命周期相关方法
     ****************************************************************************************************************/
    protected onLoad(): void {
        super.onLoad();
        this.node.on(CharacterEvent.ACTION, this.release, this);
    }
    /****************************************************************************************************************
     * Public 公有方法
     ****************************************************************************************************************/
    private release(): void {
        /**
         * 选择一个怪物
         */
        let monster = GameUtils.selectEnemy(this.character.enemySet);
        if (!monster) {
            return;
        }
        /**
         * 伤害值
         */
        let o = this.character;
        let damage = o.normalDamage();
        let value: IBaseBulletValue = { value: damage };

        let bullet = this.getBulletNode();
        o.node.addChild(bullet);

        let comp = bullet.getComponent(BaseBullet);
        comp.value = value;
        comp.source = o;
        comp.setLinearVelocity(GameUtils.shootLinearVelocity(monster.node, o.node));
        this.playAnimation();
    }
    /****************************************************************************************************************
     * private 私有方法
     ****************************************************************************************************************/

    private getBulletNode() {
        let pool: NodePool = this.pool

        if (pool.size() <= 0) {
            let add = instantiate(this.prefab);
            pool.put(add);
        }

        let node: Node = pool.get(pool);
        return node;
    }
    /****************************************************************************************************************
     * Property Get && Set 属性的 get && set 函数
     ****************************************************************************************************************/

}


